package com.hnxxxy.common.redis;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @CLASSDESCRIPER:
 * @AUTHOR:suhui
 * @PHONE:13319575368
 * @EMIAL:13319575368@163.COM
 * @DATE: 2020/5/11
 * @PARAMDESCRIPER:
 * @OUTPARAMDESCRIPER:
 */
@Slf4j
public class RedisMyabtisCstCache implements Cache {
    private ReadWriteLock read=new ReentrantReadWriteLock();
    private String id;
    private RedisTemplate<String,Object> redisTemplate;
    public RedisMyabtisCstCache(String id) {
      /* log.info("-->启动器：id"+id);*/
        this.id = id;
    }
    @Override
    public String getId() {
        return this.id;
    }
    /**
     * @Description: 存數據
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/11 15:15
     * @ACTION:
     * @param o:
     * @param o1:
     * @return: void
     **/
    @Override
    public void putObject(Object o, Object o1) {
        log.info("-->插入Redis key:"+o.toString());
        this.redisTemplate.opsForValue().set(o.toString(),o1);
    }
    /**
     * @Description: 取數據
     *  注：springboot 中原来ssm (RedisTemplate) ContextLoader.getCurrentWebApplicationContext().getBean("redisTemplate"); 获取方式取不到
     *  原因：springboot启动调用SpringBootServletInitializer中的onStartup，在onStartup初始化ContextLoader时使用的是构造函数，没有用ContextLoader.initWebApplicationContext方式
     *  那么如何来取：AutoWrited吗 当然是取不到的，因为RedisMyabtisCstCache并不是Spring容器里的bean，所以肯定AutoWrited不行，那如何来取，还是要找到spring加载Bean的源头
     *  加载Spring配置文件时，如果Spring配置文件中所定义或者注解自动注入的Bean类实现了ApplicationContextAware 接口，
     *  那么在加载Spring配置文件时，会自动调用ApplicationContextAware 接口中的方法：
     *  public void setApplicationContext (ApplicationContext context) throws BeansException
     *  通过以上的了解之后，我们来获取redisTemplate就知道该如何处理了，很简单类实现ApplicationContextAware接口就行了，然后从中获取。 然后将类设置从@Compont
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/11 15:15
     * @ACTION:
     * @param o:
     * @return: java.lang.Object
     **/
    @Override
    public Object getObject(Object o) {
        log.info("-->查询Redis key:"+o.toString());
        this.redisTemplate = ApplicationContentCollection.getBean("redisTemplate");
        Object object=this.redisTemplate.opsForValue().get(o.toString());
        return object;
    }

    /**
     * @Description: 刪除指定緩存
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/11 15:15
     * @ACTION:
     * @param o:
     * @return: java.lang.Object
     **/
    @Override
    public Object removeObject(Object o) {
        this.redisTemplate.delete(o.toString());
        return null;
    }

    /**
     * @Description: 清空
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/11 15:16
     * @ACTION:

     * @return: void
     **/
    @Override
    public void clear() {
        log.info("-->执行删除redis:"+"*"+this.id+"*");
        Set<String> keys = this.redisTemplate.keys("*"+this.id+"*");
        redisTemplate.delete(keys);
    }

    /**
     * @Description: 獲取緩存長度
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/11 15:16
     * @ACTION:
     * @return: int
     **/
    @Override
    public int getSize() {
        Set<String> keys = this.redisTemplate.keys("*"+this.id+"*");
        return keys.size();
    }

    /**
     * @Description: 读写锁
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/11 15:16
     * @ACTION:
     * @return: java.util.concurrent.locks.ReadWriteLock
     **/
    @Override
    public ReadWriteLock getReadWriteLock() {
        return read;
    }
}
